package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyMQConfig {
    /**
     * 修改消息发送对象时以json的方式发送，默认为java序列化的方式发送
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }


    //@Bean Binding Queue Exchange  将需要创建的这些组件交给容器创建 不用每次都使用AmqAdmin来创建对应的组件
    //每次启动 只要RabbitMQ里面有相同的这些组件不会进行覆盖。即使属性发生变化也不会覆盖

    @Bean
    public Queue orderDelayQueue(){

        /**
         * 设置死信队列的规则
         * x-dead-letter-exchange:order-event-exchange   设置死信队列交换机
         * x-dead-letter-routing-key:order.release.order 设置死信队列路由
         * x-message-ttl:60000 设置死信队列消息存活时间
         */


        /**
         *  Queue(String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
         *  name名字
         *  durable是否持久化
         *  exclusive是否为排他队列（如果是只能允许一个连接，其他就连不上）
         *  autoDelete是否自动删除(交换机如果没有一个绑定队列，则自动删除)
         *  arguments参数
         */
        Map<String, Object> arguments=new HashMap<>();
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        arguments.put("x-dead-letter-routing-key","order.release.order");
        arguments.put("x-message-ttl",60000);
        Queue queue = new Queue("order.delay.queue", true, false, false,arguments);

        return queue;
    }

    @Bean
    public Queue orderReleaseOrderQueue(){
        //普通队列
        Queue queue = new Queue("order.release.order.queue", true, false, false);
        return queue;
    }

    @Bean
    public Exchange orderEventExchange(){
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        TopicExchange topicExchange = new TopicExchange("order-event-exchange",true,false);
        return topicExchange;
    }

    @Bean
    public Binding orderCreateOrderBinding(){
        /**
         * Binding(String destination, Binding.DestinationType destinationType, String exchange, String routingKey,Map<String, Object> arguments)
         * destination【目的地队列】
         * Binding.DestinationType【目的地类型】
         * exchange【交换机】
         * routingKey【路由键-->交换机与队列绑定之间的路由键】
         * arguments【自定义参数】
         */
        return  new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",null);
    }

    @Bean
    public Binding orderReleaseOrderBindin(){
        return  new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",null);
    }

    /**
     * 订单释放直接和库存释放进行绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBingding(){
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "stock.release.other.#",null);
    }

    /**
     * 秒杀的队列
     * @return
     */
    @Bean
    public Queue orderSeckillOrderQueue(){
        return new Queue("order.seckill.order.queue",true,false,false);
    }

    @Bean
    public Binding orderSeckillOrderQueueBinding(){
        return new Binding("order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",null);
    }

}
